//package com.wbk.lotterysystemgateway.filter;
//
//import cn.hutool.core.io.IoUtil;
//import cn.hutool.core.lang.UUID;
//import com.coolgua.common.bean.ApiCode;
//import com.coolgua.common.bean.ApiResult;
//import com.coolgua.common.constant.RedisConstant;
//import com.coolgua.common.util.DateUtil;
//import com.coolgua.common.util.StringUtil;
//import com.coolgua.common.util.UserSessionUtil;
//import com.coolgua.config.CoolguaConfig;
//import com.coolgua.models.entity.User;
//import com.coolgua.service.FileService;
//import com.obs.services.ObsClient;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.ResponseEntity;
//import org.springframework.stereotype.Service;
//import org.springframework.util.LinkedMultiValueMap;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.client.RestTemplate;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.*;
//import java.util.Date;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
///*******************************************************************************
// * @project: rate
// * @package: com.coolgua.service.serviceImpl
// * @author: wbk
// * @created: 2021/5/1
// * @purpose:
// ******************************************************************************/
//@Service
//@Slf4j
//public class FileServiceImpl implements FileService {
//
//    @Autowired
//    CoolguaConfig coolguaConfig;
//
//    @Autowired
//    ObsClient obsClient;
//
//    @Autowired
//    RestTemplate restTemplate;
//
//    @Autowired
//    StringRedisTemplate stringRedisTemplate;
//
//    @Override
//    public String upload(String suffix, InputStream inputStream) {
//        // key格式为 20210106/uuid.jpg
//        String key = new StringBuilder(DateUtil.todayString())
//                .append("/")
//                .append(StringUtil.getUUID())
//                .append(".")
//                .append(suffix)
//                .toString();
//        return obsClient.putObject(coolguaConfig.getObs().getBucketName(), key, inputStream).getObjectUrl();
//    }
//
//    @Override
//    public String uploadFromUrl(String url) {
//        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
//        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(headers);
//        ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(url, byte[].class, requestEntity);
//        byte[] bytes = responseEntity.getBody();
//        return upload("jpg", IoUtil.toStream(bytes));
//    }
//
//    @Override
//    public ApiResult<String> adminUpload(MultipartFile file) {
//        if (file.isEmpty()) {
//            return new ApiResult<>(ApiCode.SYSTEM_FAIL.getCode(), "文件不能为空", null);
//        }
//        String originalFilename = file.getOriginalFilename();
//        File video = new File(coolguaConfig.getLocation().getTmpDir() + "/" + UUID.fastUUID() + ".mp4");
//        if (!video.getParentFile().exists()) {
//            video.getParentFile().mkdirs();
//        }
//        String key = new StringBuilder("/")
//                .append(StringUtil.getUUID())
//                .append(".")
//                .append(originalFilename.substring(originalFilename.lastIndexOf(".") + 1))
//                .toString();
//        try {
//            file.transferTo(video);
//            ProcessBuilder pb = new ProcessBuilder("/usr/bin/ffmpeg", "-i", video.getAbsolutePath(), "-vf", "drawtext=fontfile=/usr/share/fonts/simhei.ttf:text='患教大赛专家直播':x=(w-text_w):y=h-line_h-20:fontsize=24:fontcolor=white:shadowy=2", coolguaConfig.getLocation().getTmpDir() + "/" + key);
//            log.info("ffmpeg执行:{}", pb.command().toString());
//            Process process = pb.start();
//            process.waitFor();
//            InputStream err = process.getErrorStream();
//            BufferedReader br = new BufferedReader(new InputStreamReader(err));
//            String line;
//            while ((line = br.readLine()) != null) {
//                log.info(line);
//            }
//        } catch (IOException | InterruptedException e) {
//            return ApiResult.fail(ApiCode.FILE_GET_ERROR);
//        }
//        File video1 = new File(coolguaConfig.getLocation().getTmpDir() + "/" + key);
//        String url = obsClient.putObject(coolguaConfig.getObs().getBucketName(), key, video1).getObjectUrl();
//        video.delete();
//        video1.delete();
//        return ApiResult.success(url);
//    }
//
//    @Override
//    public ApiResult<String> userUpload(MultipartFile file) {
//        if (file.isEmpty()) {
//            return new ApiResult<>(ApiCode.SYSTEM_FAIL.getCode(), "文件不能为空", null);
//        }
//        String originalFilename = file.getOriginalFilename();
//        File video = new File(coolguaConfig.getLocation().getTmpDir() + "/" + UUID.fastUUID() + ".mp4");
//        if (!video.getParentFile().exists()) {
//            video.getParentFile().mkdirs();
//        }
//        String key = new StringBuilder("/")
//                .append(StringUtil.getUUID())
//                .append(".")
//                .append(originalFilename.substring(originalFilename.lastIndexOf(".") + 1))
//                .toString();
//        User user = UserSessionUtil.getCurrentUser();
//        String redisKey = RedisConstant.VIDEO_NO_PREFIX + user.getUserNo();
//        String value = DateUtil.formatDate(new Date(), "yyyyMMddHHmmssSSS");
//        stringRedisTemplate.opsForValue().set(redisKey, value, 1, TimeUnit.DAYS);
//        try {
//            file.transferTo(video);
//            ProcessBuilder pb = new ProcessBuilder("/usr/bin/ffmpeg", "-i", video.getAbsolutePath(), "-vf", "drawtext=fontfile=/usr/share/fonts/simhei.ttf:text='ID" + value + "-" + user.getName() + "':x=(w-text_w):y=h-line_h-20:fontsize=24:fontcolor=white:shadowy=2", coolguaConfig.getLocation().getTmpDir() + "/" + key);
//            log.info("ffmpeg执行:{}", pb.command().toString());
//            Process process = pb.start();
//            process.waitFor();
//            InputStream err = process.getErrorStream();
//            BufferedReader br = new BufferedReader(new InputStreamReader(err));
//            String line;
//            while ((line = br.readLine()) != null) {
//                log.info(line);
//            }
//        } catch (IOException | InterruptedException e) {
//            return ApiResult.fail(ApiCode.FILE_GET_ERROR);
//        }
//        File video1 = new File(coolguaConfig.getLocation().getTmpDir() + "/" + key);
//        String url = obsClient.putObject(coolguaConfig.getObs().getBucketName(), key, video1).getObjectUrl();
//        video.delete();
//        video1.delete();
//        return ApiResult.success(url);
//    }
//
//}
